import Wxml2Canvas from 'wxml2canvas'
import {changeDpiDataUrl} from 'changedpi'
import base64src from './base64src'
export const startDraw = ()=> {
  let that = this
        
        // 创建wxml2canvas对象
        let drawMyImage = new Wxml2Canvas({
          element: 'copyCanvas', // canvas的id,
          obj: that, // 传入当前组件的this
          width: 295,
          height: 413,
          background: '#141415', // 生成图片的背景色
          progress(percent) { // 进度
            console.log(percent);
          },
          finish(url) { // 生成的图片
            wx.hideLoading()
            savePoster(url)
          },
          error(res) { // 失败原因
            console.log(res);
            wx.hideLoading()
          }
        }, this);
        let data = {
            // 获取wxml数据
            list: [{
                type: 'wxml',
                class: '.my_canvas .my_draw_canvas',  // my_canvas要绘制的wxml元素根类名， my_draw_canvas单个元素的类名（所有要绘制的单个元素都要添加该类名）
                limit: '.my_canvas', // 要绘制的wxml元素根类名
                x: 0,
                y: 0
            }]
        }
        // 绘制canvas
        drawMyImage.draw(data, this);
}
export const drawMyCanvas = () => {
  wx.showLoading()
  const that = this
  wx.createSelectorQuery()
    .select('#copy-canvas') // 在 WXML 中填入的 id
    .fields({ scrollOffset: true, size: true }, () => {
      startDraw()
    }).exec(() => {
      console.log(888)
    })
} 
export const changeDpi = (url, dpi) => {
  return new Promise((resolve) => {
    if (dpi) {
      uni.getFileSystemManager().readFile({
          filePath: url, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => {
              // 成功的回调
              // 'data:image/jpeg;base64,'
              
              let base64 = res.data;
              let str = changeDpiDataUrl('data:image/jpeg;base64,' + base64, dpi)
              base64src(str).then(filePath => {
  
                resolve(filePath)
              })
          }
      });
    }else {
      resolve(url)
    }
  })
}
export const savePoster = (url, dpi) => {
  const that = this
  
  changeDpi(url, dpi).then(file => {
    wx.saveImageToPhotosAlbum({
        filePath: file,
        success: function() {
            wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 1500
            });
        },
        fail(err) {
          if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
            wx.showModal({
              title: '提示',
              content: '需要您授权保存相册',
              showCancel: false,
              success: modalSuccess => {
                wx.openSetting({
                  success(settingdata) {
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.saveImageToPhotosAlbum({
                            filePath: url,
                            success: function () {
                              wx.showToast({
                                title: '保存成功',
                                icon: 'success',
                                duration: 2000
                              })
                            },
                        })
                    } else {
                        wx.showToast({
                            title: '授权失败，请稍后重新获取',
                            icon: 'none',
                            duration: 1500
                        });
                    }
                  }
                })
              }
            })
          }
        }
      })
  })
      
}
